package com.horsen.service.user.controller.admin;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.horsen.common.util.R;
import com.horsen.service.user.controller.BaseController;
import com.horsen.service.user.model.DTO.MemberQueryDTO;
import com.horsen.service.user.model.Member;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 会员 控制器
 *
 * @author Horsen
 */
@Api(tags = "会员管理")
@ApiSupport(order = 2)
@Slf4j
@RestController("AdminMember")
@RequestMapping("/admin/user/members")
public class MemberController extends BaseController {

    @ApiOperationSupport(order = 1)
    @ApiOperation("获取所有会员")
    @Cacheable(key = "'adminQueryAllMembers'", value = "memberList")
    @GetMapping("/")
    public R queryAll() {
        List<Member> list = memberService.list();
        return list != null && list.size() != 0 ?
                R.ok("获取所有会员成功").setData("items", list) : R.error("获取所有会员失败");
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation("分页获取会员")
    @GetMapping("/{page}/{limit}")
    public R queryByPage(@ApiParam(value = "当前页码", required = true)
                         @PathVariable Long page,
                         @ApiParam(value = "每页记录数", required = true)
                         @PathVariable Long limit,
                         @ApiParam("会员查询对象") MemberQueryDTO memberQueryDTO) {
        IPage<Member> pageParam = memberService.pageSelect(page, limit, memberQueryDTO);
        List<Member> list = pageParam.getRecords();
        long total = pageParam.getTotal();
        return list != null ?
                R.ok("分页获取会员成功").setData("items", list).setData("total", total) : R.error("分页获取会员失败");
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation("根据ID删除会员")
    @DeleteMapping("/{id}")
    public R deleteById(@ApiParam(value = "会员ID", required = true)
                        @PathVariable String id) {
        // 删除Redis缓存
        redisTemplate.delete("adminQueryAllMembers");
        return memberService.removeById(id) ?
                R.ok("删除ID=" + id + "的会员成功") : R.error("删除ID=" + id + "的会员失败");
    }


    @ApiOperationSupport(order = 4)
    @ApiOperation("根据ID修改会员封禁状态")
    @PutMapping("/{id}")
    public R updateStatus(@ApiParam(value = "会员ID", required = true)
                          @PathVariable String id) {
        // 删除Redis缓存
        redisTemplate.delete("adminQueryAllMembers");
        return memberService.updateById(id) ?
                R.ok("解封会员成功") : R.ok("封禁会员成功");
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation("获取会员数")
    @GetMapping("/count")
    public R count() {
        Integer count = memberService.count();
        return R.ok("获取会员数成功").setData("count",count);
    }
}

